package com.Practice.Student;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Student> students = new ArrayList<>();

        System.out.println("--- 请输入3名学生的信息 ---");

        // 输入3名学生信息
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入第" + (i + 1) + "名学生的信息：");

            System.out.print("学号：");
            String idStr;
            do {
                idStr = scanner.nextLine().trim();
                if (idStr.isEmpty()) {
                    System.out.print("学号不能为空，请重新输入：");
                }
            } while (idStr.isEmpty());
            long id = Long.parseLong(idStr);

            System.out.print("姓名：");
            String name;
            do {
                name = scanner.nextLine().trim();
                if (name.isEmpty()) {
                    System.out.print("姓名不能为空，请重新输入：");
                }
            } while (name.isEmpty());

            System.out.print("年龄：");
            String ageStr;
            do {
                ageStr = scanner.nextLine().trim();
                if (ageStr.isEmpty()) {
                    System.out.print("年龄不能为空，请重新输入：");
                }
            } while (ageStr.isEmpty());
            int age = Integer.parseInt(ageStr);

            students.add(new Student(id, name, age));
            System.out.println();
        }

        // 显示所有学生信息
        System.out.println("--- 所有学生信息如下 ---");
        for (Student student : students) {
            System.out.println(student);
        }

        // equals和hashCode验证
        System.out.println("\n--- equals 和 hashCode 验证 ---");

        // 创建两个内容相同的Student对象
        Student s1 = new Student(1, "张三", 20);
        Student s2 = new Student(1, "张三", 20);
        Student s3 = new Student(2, "李四", 22);

        // 验证equals方法
        System.out.println("s1.equals(s2) 结果: " + s1.equals(s2)); // 应该为true
        System.out.println("s1.equals(s3) 结果: " + s1.equals(s3)); // 应该为false

        // 验证hashCode方法
        System.out.println("s1.hashCode(): " + s1.hashCode());
        System.out.println("s2.hashCode(): " + s2.hashCode());
        System.out.println("s1.hashCode() == s2.hashCode(): " +
                (s1.hashCode() == s2.hashCode())); // 应该为true

        // 在集合中使用验证
        List<Student> studentList = new ArrayList<>();
        studentList.add(s1);
        System.out.println("studentList.contains(s2): " + studentList.contains(s2)); // 应该为true

        scanner.close();
    }
}

